package com.miuugo.common.tools;

import com.alibaba.fastjson.JSONObject;

import java.util.*;

/**
 * 集合工具类
 */
public class CollectionsUtil {
    public static boolean isEmpty(Collection<?> collection) {
        return (collection == null || collection.isEmpty());
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return (map == null || map.isEmpty());
    }

    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    /**
     * 取差
     * @param t1
     * @param t2
     * @param <T>
     * @return
     */
    public static <T> List<T> different(List<T> t1, List<T> t2 ) {

        List<T> differentList = new ArrayList<>();

        List<T> copyt1 = new ArrayList<>(t1);

        List<T> copyt2 = new ArrayList<>(t2);

        //第一次取差
        t1.removeAll(t2);
        differentList.addAll(t1);

        //第二次取差
        copyt2.removeAll(copyt1);
        differentList.addAll(copyt2);

        return differentList;
    }

    /**
     * 分类
     * @param differentIds
     * @param originIds
     * @param addIds
     * @param deleteIds
     */
    public static void classify(List<Long> differentIds, List<Long> originIds, List<Long> addIds, List<Long> deleteIds) {
        differentIds.stream().forEach(differentId -> {
            if(originIds.contains(differentId)) {
                deleteIds.add(differentId);
            } else {
                addIds.add(differentId);
            }
        });
    }

    /**
     * 分类
     * @param differentIds
     * @param originIds
     * @param addIds
     * @param deleteIds
     */
    public static void classifyString(List<String> differentIds, List<String> originIds, List<String> addIds, List<String> deleteIds) {
        differentIds.stream().forEach(differentId -> {
            if(originIds.contains(differentId)) {
                deleteIds.add(differentId);
            } else {
                addIds.add(differentId);
            }
        });
    }

    public static void main(String[] args) {
        List<Long> differentIds = new ArrayList<>(Arrays.asList(1L,2L,3L,5L));
        List<Long> originIds = new ArrayList<>(Arrays.asList(1L,2L,3L,4L));
        List<Long> addIds = new ArrayList<>();
        List<Long> deleteIds = new ArrayList<>();

        classify(differentIds,originIds,addIds,deleteIds);
        System.out.println("differentIds:"+ JSONObject.toJSON(differentIds));
        System.out.println("originIds:"+ JSONObject.toJSON(originIds));
        System.out.println("addIds:"+ JSONObject.toJSON(addIds));
        System.out.println("deleteIds:"+ JSONObject.toJSON(deleteIds));
    }

}
